package com.beamfield.controller.manage.material;

import com.alibaba.fastjson.JSON;
import com.beamfield.common.ApiResult;
import com.beamfield.common.PageResult;
import com.beamfield.controller.common.BaseController;
import com.beamfield.enums.HttpResultEnum;
import com.beamfield.model.Issue;
import com.beamfield.model.ProjectDetail;
import com.beamfield.model.Supplies;
import com.beamfield.modelVo.IssueVo;
import com.beamfield.service.manage.sys.ProjectService;
import com.beamfield.service.manage.sys.SuppliesService;
import com.beamfield.service.manage.material.IssueService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * 发料单管理
 */
@Controller
@RequestMapping("/cms/material/issue")
public class IssueController extends BaseController {

    private final IssueService issueService;

    private final ProjectService projectService;

    private final SuppliesService suppliesService;

    @Autowired
    public IssueController(IssueService issueService,ProjectService projectService,SuppliesService suppliesService) {
        this.issueService = issueService;
        this.projectService = projectService;
        this.suppliesService = suppliesService;
    }

    /**
     * 跳转发料单管理页面
     * @return
     */
    @RequestMapping(value = "/issue",method = RequestMethod.GET)
    public ModelAndView issue(){
        return new ModelAndView();
    }

    /**
     * 跳转新增发料单页面
     * @return
     */
    @RequestMapping(value = "/addIssue",method = RequestMethod.GET)
    public ModelAndView addIssue(){
        return new ModelAndView();
    }

    @RequestMapping(value = "/editIssue")
    public ModelAndView editIssue(){
        return new ModelAndView();
    }

    /**
     * 获取新的issueId
     * @param request
     * @return
     */
    @RequestMapping(value = "/getNewIssueId",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getNewIssueId(HttpServletRequest request){
        ApiResult apiResult = new ApiResult();
        long countToday = issueService.countTodayNew();
        Long beamId = super.getBeamId(request);
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyyMMdd");
        Date date = new Date();
        String s = simpleDateFormat.format(date)+beamId+countToday;
        apiResult.setResult(s);
        return apiResult;
    }

    @RequestMapping(value = "/getIssue")
    @ResponseBody
    public ApiResult getIssue(HttpServletRequest request){
        String id = request.getParameter("id");
        if(StringUtils.isBlank(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM);
        }
        return issueService.getOneIssue(id);
    }

    @RequestMapping(value = "/getSuppliesName",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getSuppliesName(HttpServletRequest request){
        ApiResult apiResult = new ApiResult();
        long beamId = super.getBeamId(request);
        String projectId = request.getParameter("projectId");
        List<Supplies> suppliesList = suppliesService.getSuppliesDistinctByName(beamId, projectId);
        apiResult.setResult(suppliesList);
        return apiResult;
    }
    /**
     * 获取工程详情（获取施工部位）
     * @param request
     * @return
     */
    @RequestMapping(value = "/getProjectDetail",method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getProjectDetail(HttpServletRequest request){
        ApiResult apiResult = new ApiResult();
        Long beamId = super.getBeamId(request);
        String projectId = request.getParameter("projectId");
        List<ProjectDetail> projectDetails = projectService.getProjectDetails(projectId, null, null, null, beamId);
        apiResult.setResult(projectDetails);
        return apiResult;
    }

    /**
     * 根据名称获取材料
     * @param request
     * @return
     */
    @RequestMapping(value = "/getSuppliesByType", method = RequestMethod.GET)
    @ResponseBody
    public ApiResult getSupplies(HttpServletRequest request) {
        ApiResult apiResult = new ApiResult();
        long beamId = super.getBeamId(request);
        String supliesType = request.getParameter("supliesType");
        String projectId = request.getParameter("projectId");
        //施工部位
//        String projectDetailId = request.getParameter("project_detail_id");
        List<Supplies> suppliesList = issueService.getSupplies(beamId, supliesType, projectId, null);
        apiResult.setResult(suppliesList);
        return apiResult;
    }

    /**
     * 获取发料单页面数据
     * @param request
     * @return
     */
    @RequestMapping(value = "/readIssueDate",method = RequestMethod.GET)
    @ResponseBody
    public PageResult readIssueData(HttpServletRequest request){
        Long beamId = super.getBeamId(request);
        String issueName = request.getParameter("issueName");
        String offset = request.getParameter("offset");
        String limit = request.getParameter("limit");
        List<IssueVo> issueVoList = issueService.getIssueAndDetails(beamId, offset, limit, issueName);
        long total = issueService.countIssueAndDetails(beamId, issueName);
        return new PageResult(total, Long.parseLong(offset), issueVoList);
    }

    public ApiResult delIssue(HttpServletRequest request){
        String id = request.getParameter("id");
        if(StringUtils.isBlank(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM);
        }
        return issueService.delIssue(id);
    }

    /**
     * 保存发料单和发料单明细
     * @param request
     * @return
     */
    @RequestMapping(value = "/doSave",method = RequestMethod.POST)
    @ResponseBody
    public ApiResult doSave(HttpServletRequest request, String issueJson, String suppliesJson){
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        Issue issue = JSON.parseObject(issueJson, Issue.class);
        List<HashMap> suppliesDetails = JSON.parseArray(suppliesJson, HashMap.class);
        Long beamId = super.getBeamId(request);
        Long user = super.getUserId(request);
        try {
            apiResult = issueService.saveAndDetails(issue, suppliesDetails, beamId, user);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }

    @RequestMapping(value = "/doEdit")
    @ResponseBody
    public ApiResult doEdit(HttpServletRequest request, String issueJson, String suppliesJson){
        ApiResult apiResult = new ApiResult(HttpResultEnum.MIS_PARAM.getCode(),HttpResultEnum.MIS_PARAM.getMessage());
        Issue issue = JSON.parseObject(issueJson, Issue.class);
        List<HashMap> suppliesDetails = JSON.parseArray(suppliesJson, HashMap.class);
        Long beamId = super.getBeamId(request);
        Long user = super.getUserId(request);
        try {
            apiResult = issueService.updateAndDetails(issue, suppliesDetails, beamId, user);
        }catch (Exception e){
            e.printStackTrace();
            apiResult.setCode(HttpResultEnum.EXCEPTION.getCode());
            apiResult.setMessage(HttpResultEnum.EXCEPTION.getMessage());
        }
        return apiResult;
    }

    /**
     * 导出发料单
     * @param request
     */
    @RequestMapping("exportOrder")
    @ResponseBody
    public ApiResult exportOrder(HttpServletRequest request, HttpServletResponse response){
        String id = request.getParameter("id");
        if(StringUtils.isBlank(id)){
            return new ApiResult(HttpResultEnum.MIS_PARAM.getCode(), HttpResultEnum.MIS_PARAM.getMessage());
        }
        return issueService.exportOrder(id, response);
    }
}
